package com.xingda.packing.minitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.annotation.LimitRequest;
import com.xingda.packing.common.BaseController;
import com.xingda.packing.common.OperationConstant;
import com.xingda.packing.common.R;
import com.xingda.packing.annotation.OpLog;
import com.xingda.packing.minitor.dto.CacheInput;
import com.xingda.packing.minitor.dto.CleanAllCacheDto;
import com.xingda.packing.minitor.dto.PkCacheLocationDto;
import com.xingda.packing.minitor.dto.PkCacheLocationUpdateDto;
import com.xingda.packing.minitor.entity.PkAction;
import com.xingda.packing.minitor.entity.PkCacheLocation;
import com.xingda.packing.minitor.mapper.PkActionMapper;
import com.xingda.packing.minitor.service.IPkCacheLocationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@RestController
@RequestMapping("/api/cacheLocation")
public class PkCacheLocationController extends BaseController {

    @Resource
    private IPkCacheLocationService pkCacheLocationService;
    @Resource
    private PkActionMapper pkActionMapper;

    @GetMapping("/getList")
    public R getList() {
        List<PkCacheLocationDto> list = pkCacheLocationService.getPkCacheLocation(0);
        return R.ok().put("data", list);
    }

    @GetMapping("/getSortList/{area}")
    public R getSortList(@PathVariable Integer area) {
        List<List<PkCacheLocationDto>> list = pkCacheLocationService.getSortCacheLocation(area);
        return R.ok().put("data", list);
    }

    @LimitRequest(count = 1)
    @PostMapping("/clean")
    @OpLog(username = "#cacheInput.userDto.username",
            corp = "#cacheInput.userDto.corp",
            veri = "#cacheInput.userDto.veri",
            opItem = OperationConstant.CLEAN_CACHE,
            param = "#cacheInput.locationNo")
    public R cleanPkCacheLocation(@RequestBody CacheInput cacheInput) {
        try {
            LambdaQueryWrapper<PkAction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PkAction::getArea, cacheInput.getArea());
            lambdaQueryWrapper.eq(PkAction::getFinishState, 1);
            PkAction pkAction = pkActionMapper.selectOne(lambdaQueryWrapper);
            if (Objects.nonNull(pkAction)) {
                return R.error("当前存在未完成的任务,删除失败!请去后台管理页面:[操作管理-动作操作],确认完毕再进行初始化");
            }
            boolean flag = pkCacheLocationService.cleanPkCacheLocation(cacheInput.getLocationNo(), cacheInput.getArea());
            return R.ok().put("data", flag);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.error().put("data", false);
    }

    @LimitRequest(count = 1)
    @PostMapping("/cleanAll")
    @OpLog(username = "#userDto.userDto.username",
            corp = "#userDto.userDto.corp",
            veri = "#userDto.userDto.veri",
            opItem = OperationConstant.CLEAN_ALL_CACHE,
            param = "all")
    public R cleanAll(@RequestBody CleanAllCacheDto userDto) {
        System.out.println(userDto);
        try {
            boolean flag = pkCacheLocationService.cleanAll(userDto.getArea());
            return R.ok().put("data", flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error().put("data", false);
    }

    @GetMapping("/getById")
    public R getById(Integer id) {
        try {

            if (Objects.isNull(id) || id <= 0) {
                return R.error("id参数有误-id");
            }

            PkCacheLocationDto pkCacheLocationDto = pkCacheLocationService.getPkCacheLocationById(id);
            return R.ok().put("data", pkCacheLocationDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @LimitRequest(count = 1)
    @PostMapping("/updateById")
    @OpLog(username = "#cacheDto.username",
            corp = "#cacheDto.corp",
            veri = "#cacheDto.veri",
            opItem = OperationConstant.UPDATE_CACHE,
            param = "#cacheDto.id")
    public R updateById(@RequestBody PkCacheLocationUpdateDto cacheDto) {
        try {
            if (Objects.isNull(cacheDto)) {
                return R.error("参数不能为null");
            }
            if (Objects.isNull(cacheDto.getId()) || cacheDto.getId() <= 0) {
                return R.error("id参数有误-id");
            }
            if (Objects.isNull(cacheDto.getEnable()) || (cacheDto.getEnable() != 0 && cacheDto.getEnable() != 1)) {
                return R.error("enable参数有误");
            }
            PkCacheLocation pkCacheLocation = pkCacheLocationService.getById(cacheDto.getId());
            if (Objects.isNull(pkCacheLocation)) {
                return R.error("无此缓存位-id");
            }
            if (Objects.nonNull(pkCacheLocation.getWheelId())) {
                return R.error("有轮状态下，无法修改配置参数!");
            }
            pkCacheLocationService.updatePkCacheLocationBy(cacheDto);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

}
